package com.yidin.lsc;

import com.baidu.mapapi.MapActivity;
import com.yidin.logic.MainService;
import com.yidin.util.NetImageBase;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Window;

/**
 * 基类包含与后台服务连接,退出广播
 * 
 * @author cola
 * 
 */
public abstract class BaseActivity extends MapActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 接收程序退出广播
		// this.ExitCode = new downloadBroadcast();
		// IntentFilter infi = new IntentFilter("com.lsc.broadcast");
		// registerReceiver(this.ExitCode, infi);
		MainService.AddActivity(this);
	}

	private ProgressDialog m_ProgressDialog;

	public abstract void init();

	public abstract void refresh(Object... param);

	public Window getParentWindows() {
		return getParent().getWindow();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private downloadBroadcast ExitCode;

	private class downloadBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context ct, Intent it) {
			switch (it.getIntExtra("type", 0)) {
			case 1:
				finish();
				unregisterReceiver(ExitCode);
				break;
			}
		}
	}

	public void showProgress(Context context) {

		if (null == m_ProgressDialog) {
			m_ProgressDialog = new ProgressDialog(context);
			m_ProgressDialog.setMessage("正在加载，请稍候...");
			m_ProgressDialog.setCancelable(true);
		}

		m_ProgressDialog.show();
	}

	public void showProgress(Context context, String content) {
		if (null == m_ProgressDialog) {
			m_ProgressDialog = new ProgressDialog(context);
			m_ProgressDialog.setMessage(content);
			m_ProgressDialog.setCancelable(true);
		}

		m_ProgressDialog.show();
	}

	public void dismissProgress() {

		if (null != m_ProgressDialog && m_ProgressDialog.isShowing()) {
			m_ProgressDialog.dismiss();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);//
	}

	@Override
	protected void onDestroy() {
		// MainService.exitApp(BaseActivity.this);
		// Intent in = new Intent("com.lsc.broadcast");
		// in.putExtra("type", 1);
		// sendBroadcast(in);
		super.onDestroy();
	}

	protected Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 5: {
				NetImageBase item = (NetImageBase) msg.obj;

				Bitmap bm = item.getLocalBitmap(null, BaseActivity.this,
						item.path, 80, 80);
				item.ic.getImage(bm);
				break;
			}
			case 4: {
				NetImageBase item = (NetImageBase) msg.obj;

				Bitmap bm = item.getLocalBitmap(null, BaseActivity.this,
						item.path, 80, 80);
				item.ic.getImage(BitmapFactory.decodeResource(
						BaseActivity.this.getResources(), R.drawable.dt_tx));
				break;
			}
			}
		}
	};
}
